package com.aidex.biz.utils;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class AesByJinYuUtils {
    public static final String channelCode = "JXCHEYOURONG";
    private static final String KEY = "1234567890abcdef";
    private static final String AES = "AES";
    private static final String CHARSET_NAME = "UTF-8";

    public static SecretKey getKeyFromString(String key) {
        byte[] decodedKey = key.getBytes(StandardCharsets.UTF_8);
        return new SecretKeySpec(decodedKey, AES);
    }

    public static String encrypt(String plaintext) {
        try {
            Cipher cipher = Cipher.getInstance(AES);
            cipher.init(Cipher.ENCRYPT_MODE, getKeyFromString(KEY));
            byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes(CHARSET_NAME));
            return Base64.getEncoder().encodeToString(encryptedBytes);
        } catch (Exception e) {
            throw new RuntimeException("AES加密异常", e);
        }
    }

    public static String decrypt(String encryptedText) {
        try {
            Cipher cipher = Cipher.getInstance(AES);
            cipher.init(Cipher.DECRYPT_MODE, getKeyFromString(KEY));
            byte[] decodedBytes = Base64.getDecoder().decode(encryptedText);
            byte[] decryptedBytes = cipher.doFinal(decodedBytes);
            return new String(decryptedBytes, CHARSET_NAME);
        } catch (Exception e) {
            // throw new RuntimeException("AES解密异常", e);
            return null;
        }

    }


    public static String md5(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes(StandardCharsets.UTF_8));
            StringBuilder hexString = new StringBuilder();
            for (byte b : messageDigest) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) hexString.append('0');
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5 algorithm not found", e);
        }
    }

    public static void main(String[] args) {
        String data = "Ts05ChY0HOKpciLt2FzmQGDLjbuX7DyfqqgSZl4bdnTmo9/Ih6I689EgZ5rRPK/U16ONVLQwVHOU" +
                "+0z31OHGlcq7V7keHDmdPsq1YGu5pNuuyuXVaOpCNY/BeQu7cBrMvITlZAOnGM" +
                "+XMy5vTyQOtN9xRjZH4fPhiwSdStxKGjmvVS6JE9nLbIDIxEKMXeA60KUoDAjrMAk7Nc7lf78Cd09vdIc0yAYV5KG" +
                "+brs39BpNJwLvYByWqv28A0RRxyvrSrazHIBHMYjhUA5vkXXbOED7a7I+a3N" +
                "+cuwRYxYvmQINWqkErlNdm1UbqO8e9tv80cY01bTGZ7REX8dCVTyQLylwKIrQaWoReWJEPbA4R1lsHnGQmn7AJc6y1s2" +
                "/7IcrW1eKX12WwBU8pRqr8IlraNto1/3IN0651oY37an9Rj6MuzDeFrZHxJ0JJsPQsxR3FX1rqaadLZbetF6x" +
                "+bqRrXiooQnISM3GjhZ5JIx7rrFWaetrscuEY+hI0j8H2oxZRuHbbWAiTLgOpNxT1gc8fw==";
        System.out.println(AesByJinYuUtils.decrypt(data));
    }
}
